﻿'========================================================================
'功能:發送郵件參數設定,提供維護預計發送的資料,或測試目前設定的參數是否正常
'作者:andy
'日期: 2011/3/16
'========================================================================
Imports System.Data
Partial Class WebForm_Mail_SendMail
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            If Not ChkProgramRight(1) Then '查詢
                tcMain.Visible = False
            End If
            If Not ChkProgramRight(2) Then '立即送出
                btnSendNow.Visible = False
            End If

            If Not ChkProgramRight(3) Then '測試發送
                btnTest.Visible = False
            End If

          
         
            tcMain.ActiveTabIndex = 0

            ddlParamID.DataBind()
            ddlParamID.Items.Insert(0, "")


        End If

    End Sub
    ''' <summary>
    ''' 取得郵件參設的主旨.內容.回報截止天數及重送天數
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetData()

        Dim bbl As AssetManage.BBL.MailParamBBL = New AssetManage.BBL.MailParamBBL()
        Dim dt As DataTable = bbl.GetMailParamByID(ddlParamID.SelectedValue, hiDBName.Value)
        If dt.Rows.Count > 0 Then
            txtMailBody.Value = dt.Rows(0)("MailBody").ToString
            txtMailSubject.Text = dt.Rows(0)("MailSubject").ToString
            txtReturnDays.Text = dt.Rows(0)("ReturnDays").ToString
            txtResendDays.Text = dt.Rows(0)("ResendDays").ToString
            txtResendTimes.Text = dt.Rows(0)("ResendTimes").ToString
            txtMailBodyChn.Value = dt.Rows(0)("MailBodyChn").ToString
            txtMailsubjectSimplified.Text = dt.Rows(0)("MailSubjectChn").ToString
            txtMailBodyEng.Value = dt.Rows(0)("MailBodyEng").ToString
            txtMailSubjectEng.Text = dt.Rows(0)("MailSubjectEng").ToString


        End If
        
    End Sub

    
    ''' <summary>
    ''' 發送類別選項改了,相關資料也要自動抓取
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub ddlParamID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlParamID.SelectedIndexChanged
        GetData()
    End Sub



    ''' <summary>
    ''' 立即發送,Status='R'
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub btnSendNow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSendNow.Click


        Try

            If Not chkSend(False) Then
                Exit Sub
            End If

            Dim bbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()

            Dim abbl As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
            '取得符合條件的資產合計資料
            Dim ds As DataSet = abbl.GetAssetGroupData(ddlAssetType.SelectedValue, txtDept.Text, txtTakeOfficeDate.Text, ddlHouseID.SelectedValue, txtTopN.Text, txtEquQty.Text, "", "", hiDBName.Value)
            'Dim drs() As DataRow
            '沒有資料不用寄信人
            If ds.Tables(1).Rows.Count = 0 Then
                ShowMessageForAjax(btnTest, GetLocalResourceObject("MsgNoData"))
                Exit Sub
            End If
            Dim MailType As String = System.Configuration.ConfigurationManager.AppSettings("MailType")

            Dim email, MailTo As String
            MailTo = System.Configuration.ConfigurationManager.AppSettings("MailTo")
            Dim MailFrom As String = System.Configuration.ConfigurationManager.AppSettings("MailFrom")
            Dim SMTPServer As String = System.Configuration.ConfigurationManager.AppSettings("SMTPServer")

            Dim rootDir As String = String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.ApplicationPath).TrimEnd("/")
            Dim drs() As DataRow
            Dim WebReply As String = System.Configuration.ConfigurationManager.AppSettings("WebReply")
            Dim WebQuery As String = System.Configuration.ConfigurationManager.AppSettings("WebQuery")
            Dim uBBl As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
            ''先送信
            For Each dr As DataRow In ds.Tables(1).Rows '某一個員工
                drs = ds.Tables(0).Select("keeping_person='" + dr("keeping_person").ToString.Trim + "'")
                If MailType.ToLower <> "online" Then '測試
                    email = MailTo
                Else '上線
                    email = drs(0)("EMail").ToString()
                End If
                '送出信件
                ''配合語文別
                Select Case uBBl.GetLanguage(dr("keeping_person").ToString.ToLower(), hiDBName.Value)
                    Case "en-us"
                        bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, txtMailSubjectEng.Text, txtMailBodyEng.Value, hiDBName.Value, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "en-us")
                    Case "zh-cn"
                        bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, txtMailsubjectSimplified.Text, txtMailBodyChn.Value, hiDBName.Value, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "zh-cn")
                    Case Else
                        bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, txtMailSubject.Text, txtMailBody.Value, hiDBName.Value, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "zh-tw")
                End Select
            Next
            Dim ResendDays As Integer = Integer.Parse(txtResendDays.Text)
            Dim ResendTimes As Integer = Integer.Parse(txtResendTimes.Text)
            Dim ReturnDays As Integer = Integer.Parse(txtReturnDays.Text)
            Dim ReturnDeadLine As DateTime

            ''再新增紀錄到資料庫中(清除已回報資料及新增LOG)
            Dim asDS As DataSet
            For Each dr1 As DataRow In ds.Tables(1).Rows ''每一個UID
                asDS = abbl.GetAssetDataByAssettypeUID("", dr1("keeping_person").ToString, hiDBName.Value) '找出某一個人的所有資產

                ReturnDeadLine = abbl.GetWorkDate(Today, ReturnDays, dr1("keeping_person").ToString, hiDBName.Value)
                For Each dr2 As DataRow In asDS.Tables(0).Rows
                    Select Case uBBl.GetLanguage(dr1("keeping_person").ToString.ToLower(), hiDBName.Value)
                        Case "en-us"
                            abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, hiUID.Value, ReturnDeadLine, ResendDays, ResendTimes, txtMailSubjectEng.Text, txtMailBodyEng.Value, 0, hiDBName.Value) ''逐筆寫入資料

                        Case "zh-tw"
                            abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, hiUID.Value, ReturnDeadLine, ResendDays, ResendTimes, txtMailSubject.Text, txtMailBody.Value, 0, hiDBName.Value) ''逐筆寫入資料

                        Case Else
                            abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, hiUID.Value, ReturnDeadLine, ResendDays, ResendTimes, txtMailsubjectSimplified.Text, txtMailBodyChn.Value, 0, hiDBName.Value) ''逐筆寫入資料


                    End Select

                Next

            Next


        Catch ex As Exception
            WriteErrLog("SendMail", ex.Message)
            ShowMessageForAjax(btnTest, ex.Message)
            Return
        End Try

        ShowMessageForAjax(btnSendNow, GetLocalResourceObject("InsertOK"))
    End Sub

    ''' <summary>
    ''' 發信前檢查是否OK
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function chkSend(ByVal Test As Boolean) As Boolean
        If Not Test Then '不是測試時才需檢查部門的長度
            If txtDept.Text.Trim.Length < 2 Then
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrDeptNo"))
                Return False
            End If
        End If


        If ddlParamID.SelectedValue = "" Then
            ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrParamID"))
            Return False
        End If
        If txtMailSubject.Text.Trim = "" Then
            ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrMailSubject"))
            Return False
        End If
        If txtMailBody.Value.Trim = "" Then
            ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrMailBody"))
            Return False
        End If
        If txtMailBody.Value.Trim = "" Then
            ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrMailBody"))
            Return False
        End If
        Dim tmp As Integer
        Dim dd As DateTime
        If txtTopN.Text <> "" Then
            Try
                tmp = Integer.Parse(txtTopN.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrTopN"))
                Return False
            End Try
            If tmp < 0 Then
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrTopN"))
                Return False
            End If
        End If

        If txtEquQty.Text <> "" Then
            Try
                tmp = Integer.Parse(txtEquQty.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrEquQty"))
                Return False
            End Try
            If tmp < 0 Then
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrEquQty"))
                Return False
            End If
        End If




        If (txtTakeOfficeDate.Text <> "") Then
            Try
                dd = DateTime.Parse(txtTakeOfficeDate.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrTakeOfficeDate"))
                Return False
            End Try
            If dd >= Today Then
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrTakeOfficeDate"))
                Return False
            End If

        End If


        If txtReturnDays.Text <> "" Then
            Try
                tmp = Integer.Parse(txtReturnDays.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrReturnDays"))
                Return False
            End Try
            If tmp < 0 Then
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrReturnDays"))
                Return False
            End If
        End If

        If txtResendDays.Text <> "" Then
            Try
                tmp = Integer.Parse(txtResendDays.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrResendDays"))
                Return False
            End Try
            If tmp < 0 Then
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrResendDays"))
                Return False
            End If
        End If

        If txtResendTimes.Text <> "" Then
            Try
                tmp = Integer.Parse(txtResendTimes.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrResendTimes"))
                Return False
            End Try
            If tmp < 0 Then
                ShowMessageForAjax(btnSendNow, GetLocalResourceObject("ErrResendTimes"))
                Return False
            End If
        End If
        If txtMailBodyChn.Value = "" Then
            ShowMessageForAjax(btnSendNow, GetLocalResourceObject("errMailBodyChn"))
            Return False
        End If
        If txtMailBodyEng.Value.Trim = "" Then
            ShowMessageForAjax(btnSendNow, GetLocalResourceObject("errMailBodyEng"))
            Return False
        End If
        If txtMailsubjectSimplified.Text.Trim = "" Then
            ShowMessageForAjax(btnSendNow, GetLocalResourceObject("errMailSubjectChn"))
            Return False
        End If
        If txtMailSubjectEng.Text.Trim = "" Then
            ShowMessageForAjax(btnSendNow, GetLocalResourceObject("errMailSubjectEng"))
            Return False
        End If

        Return True
    End Function


    Protected Sub btnTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click

        Try


            If txtMailBox.Text.IndexOf("@") < 3 Then
                ShowMessageForAjax(btnTest, GetLocalResourceObject("errMailBox"))
                Exit Sub
            End If
            If txtAssetNo.Text.Trim = "" And txtEmpNo3.Text.Trim = "" Then
                ShowMessageForAjax(btnTest, GetLocalResourceObject("errEmpAndAsset"))
                Exit Sub
            End If





            If Not chkSend(True) Then
                Exit Sub
            End If
            Dim bbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()
            ' bbl.InsertSendMailParam(ddlParamID.SelectedValue, txtMailSubject.Text, txtMailBody.Value, txtReturnDays.Text, replyDeadline, txtResendDays.Text, hiUID.Value, txtEmpNo1.Text, txtEmpNo2.Text, txtTopN.Text, ddlDeptID1.SelectedValue, ddlDeptID2.SelectedValue, txtEquQty.Text, DateTime.Today.ToString("yyyy/MM/dd"), txtTakeOfficeDate.Text, ddlHouseID.SelectedValue, "R", hiDBName.Value)
            '   gvSendMail.DataBind()

            Dim abbl As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
            '找出符合條件的合計資產資料
            Dim ds As DataSet = abbl.GetAssetGroupData(ddlAssetType.SelectedValue, txtDept.Text, txtTakeOfficeDate.Text, ddlHouseID.SelectedValue, txtTopN.Text, txtEquQty.Text, txtEmpNo3.Text, txtAssetNo.Text, hiDBName.Value)
            'Dim drs() As DataRow
            '沒有資料不用寄信
            If ds.Tables(1).Rows.Count = 0 Then
                ShowMessageForAjax(btnTest, GetLocalResourceObject("MsgNoData"))
                Exit Sub
            End If
            Dim MailType As String = System.Configuration.ConfigurationManager.AppSettings("MailType")

            Dim MailFrom As String = System.Configuration.ConfigurationManager.AppSettings("MailFrom")
            Dim SMTPServer As String = System.Configuration.ConfigurationManager.AppSettings("SMTPServer")

            Dim rootDir As String = String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.ApplicationPath).TrimEnd("/")
            Dim drs() As DataRow
            Dim WebReply As String = System.Configuration.ConfigurationManager.AppSettings("WebReply")
            Dim WebQuery As String = System.Configuration.ConfigurationManager.AppSettings("WebQuery")

            Dim uBBl As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()

            For Each dr As DataRow In ds.Tables(1).Rows
                drs = ds.Tables(0).Select("keeping_person='" + dr("keeping_person").ToString.Trim + "'") '某1個員工的資料
                '送出測試信件
                ''配合語文別
                Select Case uBBl.GetLanguage(dr("keeping_person").ToString.ToLower(), hiDBName.Value)
                    Case "en-us"
                        bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, txtMailBox.Text, txtMailSubjectEng.Text, txtMailBodyEng.Value, hiDBName.Value, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "en-us")
                    Case "zh-cn"
                        bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, txtMailBox.Text, txtMailsubjectSimplified.Text, txtMailBodyChn.Value, hiDBName.Value, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "zh-cn")
                    Case Else
                        bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, txtMailBox.Text, txtMailSubject.Text, txtMailBody.Value, hiDBName.Value, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "zh-tw")
                End Select


            Next


            ShowMessageForAjax(btnSendNow, GetLocalResourceObject("InsertOK"))

        Catch ex As Exception
            WriteErrLog("SendTest", ex.Message)
            ShowMessageForAjax(btnTest, ex.Message)
            Return
        End Try

    End Sub


    Protected Sub btnUID_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUID.Click
        PopupFormForAjax(btnUID, "emp_no,emp_name", GetLocalResourceObject("ChnFieldsName"), "AUOREF.dbo.emp_data_all", dbName, "1,2", txtEmpNo3.ClientID & "," & txtUserName.ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")

    End Sub

    Protected Sub btnDept_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDept.Click
        Dim dt As DataTable
        Dim uBBL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        dt = uBBL.GetDept("0", hiDBName.Value)
        PopupFormForAjax(btnDept, "DeptID,DeptName", "DeptID,DeptName", dt, hiDBName.Value, "1", txtDept.ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")
    End Sub
End Class
